package com.imooc.distribution.constant;

import org.springframework.util.Assert;

/**
 * 策略模式：内部枚举实现
 */
public enum StrategyEnum {
    //指向内部枚举的同一个属性即可执行相同重复功能
    MONDAY("星期一", Type.ENGLISH),
    TUESDAY("星期二", Type.CHINESE),
    WEDNESDAY("星期三", Type.ENGLISH),

    THURSDAY("星期四", Type.CHINESE);
    private final Type type;
    private final String day;
    StrategyEnum(String day, Type type) {
        this.day = day;
        this.type = type;
    }
    /**
     * 根据k，查找并调用对应的策略
     * 未匹配上，则返回默认的策略defaultItem
     */
    public static Type match(String code){
       Assert.notNull(code,"code不能为null");
        for (StrategyEnum item: StrategyEnum.values()) {
            if (item.day.equals(code)) {
                return item.type;
            }
        }
        return Type.DEFAULT_ITEM;
    }

    /**
     * 匹配枚举+调用对应的策略方法toDo
     */
    public static String strategy(String code){
        return match(code).toDo();
    }

    /**
     * 内部策略枚举
     */
    private enum Type {
        DEFAULT_ITEM {
            @Override
            public String toDo() {
                return "";
            }
        },
        ENGLISH {
            @Override
            public String toDo() {
                return "今天上英语课";
            }
        },
        CHINESE {
            @Override
            public String toDo() {
                return "今天上语文课";
            }
        };
        public abstract String toDo();
    }

}

